<?php
/**
 * 【调度中心首页控制器】
 * Created by PhpStorm.
 * User: 孙凯
 * Date: 2018-06-014
 * Time: 15:48
 */

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;

class HomeController extends Controller
{
    /**
     * 【通知】
     * Display a listing of the resource.
     * @DateTime 2018-06-14
     * @Author 孙凯
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $search_arr = $request->all();
        $query = DB::table('message')->join('message_list', 'message.id', '=', 'message_list.info_id');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case '1':
                        $query->get();
                        break;
                    case '2':
                        $query->where('message.name', '=', '订单通知');
                        break;
                    case '3':
                        $query->where('message.name', '=', '司机订单');
                        break;
                    case '4':
                        $query->where('message.name', '=', '报价审批');
                        break;
                    case '5':
                        $query->where('message.name', '=', '司机审批');
                        break;
                }
            }
        })->where('message.status', '=', 1)
            ->orderBy('message.update_time', 'desc')
            ->select('message.name', 'message.id', 'msg_contents')
            ->get()->toarray();
        // dd($data);
        $data = json_decode(json_encode($data), true);
        return $data ? response(['msg' => $data, 'message' => '数据获取成功', 'code' => '1000'], 200) : response(['msg' => '', 'message' => '数据已经全部处理，你好棒啊！', 'code' => '1011'], 400);
    }


    /**
     * 【概览未处理订单】
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function read(Request $request)
    {
        $search_arr = $request->all();
        $query = DB::table('message');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case '1':
                        $query->where('message.name', '=', '订单通知');
                        break;
                }
            }
        })->where('message.status', '=', 1)
            ->select('name')->count();
        $data = json_decode(json_encode($data), true);
        // dd($data);
        return response(['msg' => $data, 'message' => $data . '条未处理订单', 'code' => '1000'], 200);
    }

    /**
     * 【概览已经处理订单】
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function unread(Request $request)
    {
        $search_arr = $request->all();
        $query = DB::table('message');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case '1':
                        $query->where('message.name', '=', '订单通知');
                        break;
                }
            }
        })->where('message.status', '=', 2)
            ->select('name')->count();
        $data = json_decode(json_encode($data), true);
        return response(['msg' => $data, 'message' => $data . '条未处理订单', 'code' => '1000'], 200);
    }

    /**
     * 【概览未处理审批】
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $search_arr = $request->all();
        $query = DB::table('message');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case 2:
                        $query->where('name', 'like', '%' . '审批' . '%');
                        break;
                }
            }
        })->where('message.status', '=', 1)
            ->select('name')->count();
        $data = json_decode(json_encode($data), true);
        return response(['msg' => $data, 'message' => $data . '条未处理订单', 'code' => '1000'], 200);
    }

    /**
     * 【概览yij处理审批】
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function unstore(Request $request)
    {

        $search_arr = $request->all();
        $query = DB::table('message');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case 2:
                        $query->where('name', 'like', '%' . '审批' . '%');
                        break;
                }
            }
        })->where('message.status', '=', 2)
            ->select('name')->count();
        $data = json_decode(json_encode($data), true);
        return response(['msg' => $data, 'message' => $data . '条数据', 'code' => '1000'], 200);
    }

    /**
     * 【弹窗内容】
     * Display a listing of the resource.
     * @DateTime 2018-06-14
     * @Author 孙凯
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $search_arr = $request->all();
        $query = DB::table('message')->join('message_list', 'message.id', '=', 'message_list.info_id');//日志表
        $data = $query->where(function ($query) use ($search_arr) {
            foreach ($search_arr as $search) {
                switch ($search) {
                    case '1':
                        $query->where('message.name', '=', '订单通知');
                        break;
                }
            }
        })->where('message.status', '=', 1)
            ->select('message.name', 'message.id as mid', 'msg_contents')->get()->toarray();
        $data = json_decode(json_encode($data), true);
        // dd($data);
        if (empty($data)){
            return response(['msg' => '全部处理完了', 'type' => '弹窗显示', 'message' => '订单通知获取成功', 'code' => '1000'], 200);
        }
        return response(['msg' => $data, 'type' => '弹窗显示', 'message' => '订单通知获取成功', 'code' => '1000'], 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $data = $request->all();
        //$data= json_decode($data);
        if (empty($data['mid'])) {
            return response(['msg' => $data, 'message' => '参数不完整', 'code' => '1011'], 400);
        }
        $id = $data['mid'];
        $query = DB::table('message')->where('id', '=', $id)
            ->update(['status' => 2]);
        $zt = DB::table('message_list')->where('info_id', '=', $id)
            ->update(['status' => 2]);
        // $data = json_decode(json_encode($query), true);
        if ($query === 1 && $zt === 1) {
            return response(['msg' => $data, 'message' => 'id=' . $id . '订单通知操作成功', 'code' => '1000'], 200);
        } else {
            return response(['msg' => $data, 'message' => 'id=' . $id . '的订单', 'code' => '1000'], 200);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}
